<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>护照首页</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<!--标准mui.css-->
		<link rel="stylesheet" href="../css/mui.min.css">
		<link rel="stylesheet" href="../css/common.css" />
		<style>
			* {
				margin: 0px;
				padding: 0px;
			}
			body {
				background-color: #FFFFFF;
			}
			.mui-content {
				background-color: #FFFFFF;
			}
			.header-container {
				margin-bottom: 20px;
				width: 100%;
				text-align: -webkit-center;
			}
			.header-div {
				margin-top: 30px;
				width: 200px;
				height: 200px;
				border: 1px solid #F3F3F3;
				text-align: -webkit-center;
			}
			.camera-img {
				width: 40px;
				height: 40px;
				margin-top: 80px;
				background-image: url(../img/detail/camera_pressed.png);
				background-size: 100% 100%;
			}
			.camera-img input::-webkit-file-upload-button {
	            width: 55px;
	            height: 55px;
	            border: none;
	            position: absolute;
	            outline: 0;
	            opacity: 0;
	        }
	        .camera-img input#file {
	            display: block;
	            width: auto;
	            border: 0;
	            vertical-align: middle;
	        }
	        .line-div {
				width: 100%;
				height: 1px;
				background-color: #F3F3F3;
			}
			.tips-container {
	        		margin-top: 20px;
	        		margin-left: 25px;
	        }
	        .tips-container .title {
	        		font-family: 'PingFangSC-Medium';
				font-size: 16px;
				color: #4A4A4A;
				margin-bottom: 5px;
	        }
	        .tips-container .content {
	        		margin-left: 12px;
	        		font-family: 'PingFangSC-Regular';
				font-size: 12px;
				color: #4A4A4A;
				line-height: 25px;
	        }
	        .sample-div {
	        		margin-top: 15px;
				width: 200px;
				height: 200px;
	        }
	        .sample-img {
	        		width: 200px;
				height: 200px;
	        }
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">护照首页</h1>
		</header>
		<div class="mui-content">
			<div class="header-container">
				<div class="sample-div">
					<img class="sample-img" src="../img/sample/huzhao_img.png" />
				</div>
				<div class="header-div">
					<div class="camera-img">
						<input type="file" accept="image/*" onchange="handleInputChange(this);" />
					</div>
				</div>
			</div>
			<div class="line-div"></div>
			<div class="tips-container">
				<div class="title">资料要求</div>
				<div class="content">.有效期离出发日至少还有6个月</div>
				<div class="content">.露出四个边角，清晰拍张照，不要反光</div>
			</div>
		</div>
	</body>
	<script type="text/javascript" src="../js/thirdparty/jquery-3.3.1.js" ></script>
	<script type="text/javascript" src="../js/thirdparty/mui.min.js" ></script>
	<script type="text/javascript" src="../js/thirdparty/spin.min.js" ></script>
	<script type="text/javascript">
		document.write('<script type="text/javascript" src="../js/utils/boot.js?v='+new Date().getTime()+'" ><\/script>');
	</script>
	<script type="text/javascript">
		var customerId = base.getParameter("customerId");
		var applyCustomerId = base.getParameter("applyCustomerId");
		var passportId = "";
		$(function() {
			getPassportInfoData();
		})
		function getPassportInfoData() {
			base.setWait("正在加载数据");
			var url = base.url.getPassportInfo;
			var data = {
				applyCustomerId: applyCustomerId
			};
			base.postData(url,data,getPassportInfoSuccess);
		}
		function getPassportInfoSuccess(data) {
			if (data.success) {
				if (data.extendData.passportInfo) {
					passportId = data.extendData.passportInfo.id;
					$(".header-div").css("background-image", "url("+data.extendData.passportInfo.passportImg+")");
					$(".header-div").css("background-size", "100% 100%");	
				}
			} else {
				mui.toast(data.message);
			}
		}
		function handleInputChange(file) {
			var base64, file = file.files[0];
			var maxSize = 1024 * 1024 * 8; //最大不超过8M
			var imgSize = file.size;
			if(maxSize < imgSize) {
				alert('上传图片不能超过8M');
				file.value = '';
				return
			}
			var reader = new FileReader();
			reader.onload = function(e) {
				base64 = e.target.result;
				canvasDataURL(base64);
			}
			reader.readAsDataURL(file);
		}
		//利用canvas压缩图片，根据画布大小和图像质量压缩
		function canvasDataURL(base64) {
			var img = new Image();
			img.src = base64;
			img.onload = function() {
				//默认按照比例压缩
				var scale = this.width / this.height;
				//规定压缩后的大小
				var canvasWidth = 600;
				var canvasHeight = canvasWidth / scale;
				//生成canvas
				var canvas = document.createElement('canvas');
				var ctx = canvas.getContext('2d');
				//创建节点属性
				canvas.width = canvasWidth;
				canvas.height = canvasHeight;
				var anw = document.createAttribute('width');
				anw.nodeValue = canvasWidth;
				var anh = document.createAttribute('height');
				anh.nodeValue = canvasHeight;
				canvas.setAttributeNode(anw);
				canvas.setAttributeNode(anh);
				ctx.drawImage(this, 0, 0, canvasWidth, canvasHeight);
				//图像质量,值越小，所绘制出的图像越模糊
				var quality = 0.92;
				var base64String = canvas.toDataURL('img/jpeg', quality);
				//通过base64获取二进制文件
				var blob = getBlobByBase64(base64String);
				//上传图片
				uploadImg(blob);
			}
		}
		//通过base64获取二进制文件
		function getBlobByBase64(base64String) {
			var arr = base64String.split(','),
				mine = arr[0].match(/:(.*?);/)[1],
				bstr = atob(arr[1]),
				n = bstr.length,
				u8arr = new Uint8Array(n);
			while(n--) {
				u8arr[n] = bstr.charCodeAt(n);
			}
			return new Blob([u8arr], {
				type: mine
			});
		}	
		//上传图片
		function uploadImg(blob){
			base.setWait("正在上传，请稍等...");
			var url = root_host + 'ry-web-admin/api/passport/addPassport'; //需要上传的接口地址
			var fd = new FormData();
			//上传文件参数(看接口需要传什么)
			fd.append('file', blob, 'authonValidate.jpeg');
			fd.append('customerId', customerId);
			fd.append('applyCustomerId', applyCustomerId);
			fd.append('passportId', passportId);
			//XMLHttpRequest
			var xhr = new XMLHttpRequest();
			xhr.open('post', url);
			//请求头参数（看接口需要传什么）
			//		    xhr.setRequestHeader('token',sessionStorage.getItem('token'));
			//		    xhr.setRequestHeader('comId',sessionStorage.getItem('comId'));
			xhr.onreadystatechange = function() {
				console.log(xhr);
				if(xhr.readyState != 4) {
					return;
				}
				var data = JSON.parse(xhr.responseText);
				base.clearWait();
				if(data.success) {
					if (Object.keys(data.extendData).length) {
						passportId = data.extendData.passportInfo.id;
						$(".header-div").css("background-image", "url("+data.extendData.passportInfo.passportImg+")");
						$(".header-div").css("background-size", "100% 100%");	
					}
				} else {
					mui.toast(data.message);
				}
			}
			xhr.send(fd);
		}
	</script>
</html>